<?php

class Emms_Form_Admin_PerformanceMetric extends HTech_Form
{
    protected $_primary  = 'PerformanceMetricId';
    
    public function __construct() 
    {
        parent::__construct();
        
        $this->setName('PerformanceMetric');
        
        $this->setAttrib('title', $this->translate("Performance metric form"));
        $this->setDescription($this->translate("Edit performance metric info"));
        
        $pmId = new Zend_Form_Element_Hidden('PerformanceMetricId');
        
        $subject = new Zend_Form_Element_Text('Subject');
        $subject->setLabel("Subject")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $metric = new Zend_Form_Element_Text('Metric');
        $metric->setLabel("Metric")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $markA = new Zend_Form_Element_Text('A');
        $markA->setLabel("Mark A")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid mark")))
            ->addValidator('Callback', true, array(
                'callback' => array($this, 'checkMarkCallback'), 
                'messages' => $this->translate("Mark A must be lower than Mark B and Mark C")));
        
        $markB = new Zend_Form_Element_Text('B');
        $markB->setLabel("Mark B")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid mark")))
            ->addValidator('Callback', true, array(
                'callback' => array($this, 'checkMarkCallback'), 
                'messages' => $this->translate("Mark B must be greater than Mark A and lower than Mark C")));
        
        $markC = new Zend_Form_Element_Text('C');
        $markC->setLabel("Mark C")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid mark")))
            ->addValidator('Callback', true, array(
                'callback' => array($this, 'checkMarkCallback'), 
                'messages' => $this->translate("Mark C must be greater than Mark A and Mark B")));
        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type'=>'submit')));
        
        $this->addElements(array($subject,$metric,$markA,$markB,$markC,$submit,$pmId));
        
    }
    
    public function checkMarkCallback($mark, $args) 
    {
        if($args['A'] >= $args['B']) return false;
        if($args['B'] >= $args['C']) return false;
        return true;
    }
}